//
//  QFVistorView.swift
//  sinaWeibo
//
//  Created by apple on 16/2/26.
//  Copyright © 2016年 Qifeng Yan. All rights reserved.
//

import UIKit

// 设置代理协议
protocol QFVistorViewDelegate:NSObjectProtocol {
    // 创建代理方法
    func vistorViewDidLoginButtonClick()        // 登陆按钮的代理方法
    func vistorViewDidRegisterButtonClick()     // 注册按钮的代理方法
}

class QFVistorView: UIView {
    
    // 设置代理属性
    weak var vistorDelegate: QFVistorViewDelegate?

    override init(frame: CGRect) { // 重写父类的init 方法 在初始化的时候加载控件
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1) // 设置背景颜色
        
        setupUI() // 加载控件
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // MARK: - 布局子控件
    func setupUI() {
        // 将加载的控件添加到父控件中
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 设置约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLable.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 设置转轮的约束
        // 转轮的CenterX
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem:self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 转轮的centerY
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 小房子CenterX 和转轮的 CenterX 重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // CenterY 和转轮的 CenterY 重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // messageLable
        // 长度设置为240 
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // 设置messageLable的顶部距离 转轮的底部 16
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // CenterX和转轮的CenterX重合
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 设置注册按钮
        // 注册按钮的左边与messageLable 的左边对齐 
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        // 注册按钮的顶部与messaLab 的底部距离16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 注册按钮的宽度是 100 
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 注册按钮的高度是 35
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登陆按钮
        // 登陆按钮的右边与messageLable的右边对齐
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        // 登陆按钮的顶部与注册按钮的顶部对齐
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        // 登陆按钮的大小 和 注册按钮一样
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        // 设置coverView的约束
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
    }
    
    // MARK: - 不同的访客界面不同
    func setupVisitorInfo (title: String, imageName: String) {
        messageLable.text = title                   // 文字类容
        messageLable.sizeToFit()
        iconView.image = UIImage(named: imageName)  // 替换图片
        homeView.hidden = true                      // 将小房子隐藏
        sendSubviewToBack(coverView)                // 将遮罩置于最底层
    }
    
    // MARK: - 开启首页动画
    func startRotationAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation") // 创建核心动画
        animation.toValue = 2 * M_PI    // 设置旋转角度
        animation.duration = 20         // 设置旋转时间
        animation.repeatCount = MAXFLOAT // 设置旋转次数（最大数）
        
        animation.removedOnCompletion = false  // 防止切换到首页的时候动画停止
        
        iconView.layer.addAnimation(animation, forKey: "QFHomeViewController") // 添加动画
        
        
    }
    
    // MARK: - 懒加载
    // 小房子
    lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 转轮
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 消息Lable
    lazy var messageLable: UILabel = {
        let lable = UILabel()
        
        // 设置Lable
        lable.text = "关注一些人,看看上面有惊喜"         // 文本内容
        lable.font = UIFont.systemFontOfSize(16)     // 文字大小
        lable.textAlignment = NSTextAlignment.Center // 对齐方式
        lable.textColor = UIColor.darkGrayColor()    // 字体颜色
        lable.numberOfLines = 0                      // 文字换行
        
        return lable
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
       let button = UIButton()
        
        // 设置button属性
        button.setTitle("注册", forState: UIControlState.Normal) // 设置文字
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal) // 设置字体颜色
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal) //设置背景图片
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside) // 添加注册按钮的点击事件
        
        return  button
    }()
    
    // 登陆按钮
    lazy var loginButton: UIButton = {
        let button = UIButton()
        
        // 设置button舒心
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal) // 设置字体颜色
        button.setTitle("登陆", forState: UIControlState.Normal) // 设置文字
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal) // 设置背景图片
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)  // 添加登陆按钮的点击事件
        
        return button
    }()
    
    // 懒加载遮盖
    lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // MARK: - 监听登陆按钮 和 注册按钮的点击事件
    func loginButtonClick() {
        vistorDelegate?.vistorViewDidLoginButtonClick() // 调用代理方法
    }
    
    func registerButtonClick() {
        vistorDelegate?.vistorViewDidRegisterButtonClick()
    }
}
